using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Admin
{
    public partial class StudentData : Form
    {
        List<Student> all_stu;
        List<Student> stu_added = new List<Student>();
        int pagenumber = 1;
        int pageSize = 20;
        int totalCount = 0;
        int pageCount = 0;

        public StudentData()
        {
            InitializeComponent();
        }

        private void import_stu_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            string Path = openFileDialog1.FileName;
            DataTable dt = Tools.Read_excel.read_excel(Path);
            if (dt == null) return;

            List<Student> students = new List<Student>();

            foreach (DataRow dr in dt.Rows)
            {
                Student stu = new Student();
                stu.grade = -1;
                stu.phone = dr["手机号码"].ToString();
                stu.stu_no = dr["学号"].ToString();
                stu.stu_name = dr["姓名"].ToString();
                stu.pwd = "123";
                stu.major_id = -1;
                stu.group_id = -1;
                students.Add(stu);
            }
            BLL.B_Student b_stu = new BLL.B_Student();
            try
            {
                var res = b_stu.Add(students);
                pagenumber = 1;
                getPage(ref totalCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            BLL.B_Student b_Student = new BLL.B_Student();
            if (b_Student.save_List(all_stu))
                MessageBox.Show("保存成功！");
        }

        private void StudentData_Load(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(uiButton4, "上一页");
            toolTip1.SetToolTip(uiButton5, "上一行");
            toolTip1.SetToolTip(uiButton6, "下一页");
            toolTip1.SetToolTip(uiButton7, "下一行");
            pagenumber = 1;
            getPage(ref totalCount);
        }

        void getPage(ref int totalCount)
        {
            BLL.B_Student b_Student = new BLL.B_Student();

            all_stu = b_Student.GetPage(pagenumber, pageSize, ref totalCount);
            students.DataSource = new BindingList<Student>(all_stu);
            students.Columns["pwd"].Visible = false;
            pageCount = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        }

        private void students_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            var stu = students.CurrentRow.DataBoundItem as Student;
            stu.group_id = -1;
            stu_added.Add(stu);
        }

        private void Del_Click(object sender, EventArgs e)
        {
            List<Student> stu_deleted = new List<Student>();
            foreach(DataGridViewRow row in students.SelectedRows) 
            {
                stu_deleted.Add(row.DataBoundItem as Student);
            }
            BLL.B_Student b_Student = new BLL.B_Student();
            if (b_Student.Del(stu_deleted))
                MessageBox.Show("删除成功！");

            getPage(ref totalCount);
        }

        private void First_Click(object sender, EventArgs e)
        {
            pagenumber = 1;
            getPage(ref totalCount);
        }

        private void Previous_Click(object sender, EventArgs e)
        {
            if (pagenumber > 1)
            {
                pagenumber--;
                getPage(ref totalCount);
            }
        }

        private void Next_Click(object sender, EventArgs e)
        {
            if (pagenumber < pageCount)
            {
                pagenumber++;
                getPage(ref totalCount);
            }
        }

        private void Last_Click(object sender, EventArgs e)
        {
            pagenumber = pageCount;
            getPage(ref totalCount);
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            string Path = openFileDialog1.FileName;
            DataTable dt = Tools.Read_excel.read_excel(Path);
            if (dt == null) return;

            List<Student> students = new List<Student>();

            foreach (DataRow dr in dt.Rows)
            {
                Student stu = new Student();
                stu.grade = -1;
                stu.phone = dr["手机号码"].ToString();
                stu.stu_no = dr["学号"].ToString();
                stu.stu_name = dr["姓名"].ToString();
                stu.pwd = "123";
                stu.major_id = -1;
                stu.group_id = -1;
                students.Add(stu);
            }
            BLL.B_Student b_stu = new BLL.B_Student();
            try
            {
                var res = b_stu.Add(students);
                pagenumber = 1;
                getPage(ref totalCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            List<Student> stu_deleted = new List<Student>();
            foreach (DataGridViewRow row in students.SelectedRows)
            {
                stu_deleted.Add(row.DataBoundItem as Student);
            }
            BLL.B_Student b_Student = new BLL.B_Student();
            if (b_Student.Del(stu_deleted))
                MessageBox.Show("删除成功！");

            getPage(ref totalCount);
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            BLL.B_Student b_Student = new BLL.B_Student();
            if (b_Student.save_List(all_stu))
                MessageBox.Show("保存成功！");
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            pagenumber = 1;
            getPage(ref totalCount);
        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
            if (pagenumber > 1)
            {
                pagenumber--;
                getPage(ref totalCount);
            }
        }

        private void uiButton6_Click(object sender, EventArgs e)
        {
            pagenumber = pageCount;
            getPage(ref totalCount);
        }

        private void uiButton7_Click(object sender, EventArgs e)
        {
            if (pagenumber < pageCount)
            {
                pagenumber++;
                getPage(ref totalCount);
            }
        }
    }
}
